package com.edu.cdp.service.impl;

import com.edu.cdp.DTO.AddGoodsDTO;
import com.edu.cdp.DTO.PageDTO;
import com.edu.cdp.DTO.UpdateGoodDTO;
import com.edu.cdp.common.PageResult;
import com.edu.cdp.service.CommonService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.cdp.domain.Goods;
import com.edu.cdp.service.GoodsService;
import com.edu.cdp.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Yang Can
* @description 针对表【goods】的数据库操作Service实现
* @createDate 2024-05-30 10:20:26
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService{
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CommonService commonService;

    @Override
    public Goods newUpdate(UpdateGoodDTO goodsDTO, MultipartFile file) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>();
        queryWrapper.eq("id", goodsDTO.getId());

        Goods oldGoods = goodsMapper.selectOne(queryWrapper);


        if (oldGoods!= null) {
            if (file != null) {
                oldGoods.setGoodImage(commonService.upload(file));
            }

            if (goodsDTO.getGoodsName() != null && goodsDTO.getGoodsName() != ""){
                oldGoods.setGoodsName(goodsDTO.getGoodsName());
            }

            if (goodsDTO.getPrice() != null && goodsDTO.getPrice() > 0){
                oldGoods.setPrice(goodsDTO.getPrice());
            }

            if (goodsDTO.getCategoryId() != null && goodsDTO.getCategoryId() > 0){
                oldGoods.setCategoryId(goodsDTO.getCategoryId());
            }

            return oldGoods;
        } else {
            return null;
        }
    }

    @Override
    public Goods add(AddGoodsDTO goods, MultipartFile file) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>();
        queryWrapper.eq("goods_name", goods.getGoodsName());
        queryWrapper.eq("category_id", goods.getCategoryId());

        Goods existingGood = goodsMapper.selectOne(queryWrapper);

        if (existingGood != null) {
            log.error("商品已存在: 名称：" + existingGood.getGoodsName() +
                    ", 分类I：" + existingGood.getCategoryId() );
            return null;
        }else {
            Goods good = new Goods();
            if (file != null && !file.isEmpty()) {
                try {
                    String filePath = commonService.upload(file);
                    good.setGoodImage(filePath);
                } catch (Exception e) {
                    log.error("文件上传失败: " + e.getMessage());
                    return null; // 或者根据业务需求处理异常
                }
            } else {
                good.setGoodImage(null);
            }
            good.setGoodsName(goods.getGoodsName());
            good.setStock(goods.getStock());
            good.setPrice(goods.getPrice());
            good.setCategoryId(goods.getCategoryId());

            goodsMapper.insert(good);

            return good;
        }
    }

    @Override
    public List<Goods> listByTypeId(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type_id", id);

        return goodsMapper.selectByMap(map);
    }

    @Override
    public List<Goods> selectByStock(Integer quantity) {
        LambdaQueryWrapper<Goods> query = new QueryWrapper<Goods>()
                .lambda().le(Goods::getStock, quantity);

        return goodsMapper.selectList(query);
    }

    @Override
    public List<Goods> listByName(String name) {
        QueryWrapper<Goods> query = new QueryWrapper<Goods>();
        query.like("goods_name", name);

        return goodsMapper.selectList(query);

    }

    @Override
    public List<Goods> selectByStockAmple(Integer quantity) {
        LambdaQueryWrapper<Goods> query = new QueryWrapper<Goods>()
                .lambda().ge(Goods::getStock, quantity);

        return goodsMapper.selectList(query);
    }

    @Override
    public PageResult pageQuery(PageDTO goodsPageDTO) {
        Page<Goods> page = new Page<>(goodsPageDTO.getPageNum(), goodsPageDTO.getPageSize());
        IPage<Goods> goodsPage = goodsMapper.selectPage(page,null);

       PageResult pageResult = new PageResult();
       pageResult.setList(goodsPage.getRecords());
       pageResult.setTotal(goodsPage.getTotal());

        return pageResult;
    }

    @Override
    public Goods selectByCategoryId(Integer categoryId) {
        QueryWrapper<Goods> query = new QueryWrapper<Goods>();
        query.eq("category_id", categoryId);

        return goodsMapper.selectOne(query);
    }

    @Override
    public Goods addStock(Integer id, Integer quantity) {
        if (quantity < 0) {
            log.error("新增数量不能小于0");
            return null;
        }

        Goods good = goodsMapper.selectById(id);
        if (good == null) {
            log.error("查无此商品");
            return null;
        } else {
            if (good.getStock() >= 100) {
                log.error("库存已满");
                return null;
            }
            if (good.getStock() + quantity >= 100) {
                good.setStock(100);
                log.error("所加数量超过限制");
            } else {
                good.setStock(good.getStock() + quantity);
            }
        }

        //修改库存
        goodsMapper.updateById(good);
        return good;
    }
}




